/*
Content: Table G.1
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

********************************************************************************
**# LOAD DATA
********************************************************************************

use "$final/finaldata_ieb_main_light.dta", replace

********************************************************************************
**# MERGE OCCUPATIONAL CHARACTERISTICS (constructed in xxx file)
********************************************************************************
		
// NOTE FOR THOSE WHO DO NOT MATCH WE ASSIGN THE YEAR-OCC LEVEL OR THE OCC LEVEL ALONE
	
foreach x in  "_fa" "_lb" {

foreach data in "" "_yearocc" "_occ" {

preserve 
use "$help/occupational_scores_2perc_germans`data'.dta", clear

foreach var in	educ_max_d1 educ_max_d2 educ_max_d3 tentgelt ln_hwage lnwage haupttask_d1 haupttask_d2 haupttask_d3 haupttask_d4 haupttask_d5 regulindex ieb_beruf_kons_num {

rename 	`var' `var'`x'

}

cap rename ao_land woao_land


tempfile occscore`x'

save `occscore`x''

restore

cap merge m:1 ieb_beruf_kons_num`x' year woao_land using `occscore`x'', nogen keep(1 3)		// occ-year-land
cap merge m:1 ieb_beruf_kons_num`x' year using `occscore`x'', nogen update keep(1 3 4 5)	// occ-year
cap merge m:1 ieb_beruf_kons_num`x' using `occscore`x'', nogen update keep(1 3 4 5)			// occ

}
}
		
		

* ONLY NON MISSINGS ON BASELINE JOB AND FIRM OUTCOMES
cap drop alloutcomes_job alloutcomes_firm
g alloutcomes_job=1 if  ln_wage_any_fa!=. & full_time_fa!=. & ieb_beruf_kons_num_fa!=. 
g alloutcomes_firm=1 if ln_firmsize_fa != . & share_for_fa_ab_med !=. & qualif_hq_fa_ab_med !=. & abovemedian_AKM_fa !=.
	

********************************************************************************
**# TABLE G.1
********************************************************************************
cap clonevar ln_firmsize_lb = ln_firmsize

eststo clear
local i=1
foreach out in educ_max_d1 educ_max_d2 educ_max_d3 ln_hwage lnwage haupttask_d1 haupttask_d2 haupttask_d3 haupttask_d4 haupttask_d5 regulindex {
	
qui eststo mod`i':  reghdfe `out'_fa treatment_terror_m3y_p90 i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize_lb  full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb `out'_lb  if  unemp_3m==1 & register_unemp==1 & k_treatment_terror_m3y_p90_st==1 & alloutcomes_job == 1, absorb(i.ieb_nat_id#i.year2 i.month#i.year2 i.state ) vce(cluster clusterterror_m3y_p90)

sum	 `out'_fa if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & treatment_terror_m3y_p90 == 0
    estadd scalar ymean = r(mean)
    estadd local control_ext "Yes"
    estadd local yearorigin "Yes"
    estadd local yearmonth "Yes"
    estadd local state "Yes"
    local i=`i'+1
}

local i=`i'+1
	}

esttab mod* using "$table/tableG1_final.tex", star(* 0.10 ** 0.05 *** 0.01) se replace b(3) ///
drop(*edu_max age frau *ysm_atunemp ln_wage_any_lb ln_firmsize_lb full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb ///
educ_max_d1_lb educ_max_d2_lb educ_max_d3_lb ln_hwage_lb lnwage_lb haupttask_d1_lb haupttask_d2_lb haupttask_d3_lb haupttask_d4_lb haupttask_d5_lb regulindex_lb _cons) nocons ///
mgroups("Max education" "Occupational score" "Main task" "Degree of occupational regulation", ///
pattern(1 0 0 1 0 1 0 0 0 0 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
mtitles("Low" "Middle" "High" "Ln hourly" "Ln daily" "Analytic non-routine" "Interactive non-routine" "Cognitive routine" "Manual routine" "Manual non-routine" "Regulindex") ///
coeflabel(treatment_terror_m3y_p90 "Post-Terror") ///
stats(N ymean yearorigin yearmonth state control_ext, ///
fmt(%12.0gc %5.3f) ///
label("Observations" "Avg Outcome" "Year*Origin FE" "Year*Month FE" "State FE" "Extended Controls")) ///
label nonotes addnote("Robust SE, *p<.1; **p<.05; ***p<.01") ///
fragment

